//
//  YearToDayTableViewCell.m
//  Kaiser
//
//  Created by Fidetro on 2017/1/3.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "YearToDayTableViewCell.h"

@interface YearToDayTableViewCell ()
{
    UIToolbar *_inputAccessoryView;
    UIPickerView *_inputView;
}
@end

@implementation YearToDayTableViewCell
-(BOOL)canBecomeFirstResponder{
    
    return YES;
    
}
- (instancetype)init{
    
    
    if ([super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kYearToDayCellIdentifier]) {
        
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.textLabel.textColor = [UIColor blackColor];
        [self.textLabel setFont:[UIFont systemFontOfSize:16.f]];
        self.detailTextLabel.textColor = [UIColor colorWithHexString:@"666666"];
        [self.detailTextLabel setFont:[UIFont systemFontOfSize:16.f]];
        self.backgroundColor = [UIColor clearColor];


    }
    
    return self;
    
}

-(UIView *)inputView{
    
    if (!_inputView) {
        
        self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 200, SCREEN_WIDTH, 200)];
        self.datePicker.timeZone = [NSTimeZone localTimeZone];
        self.datePicker.maximumDate = [NSDate date];
        self.datePicker.datePickerMode = UIDatePickerModeDate;
        self.datePicker.backgroundColor = [UIColor whiteColor];
        return self.datePicker;
        
    }
    
    return _inputView;
    
}

- (UIView *)inputAccessoryView{
    
    if (!_inputAccessoryView) {
        
        
        UIView *toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        
        toolBar.backgroundColor = [UIColor colorWithHexString:@"f8f7f7"];
        UIView *lineView = [[UIView alloc]init];
        UIButton *cancelButton = [[UIButton alloc]init];
        UIButton *confirmButton = [[UIButton alloc]init];
        [toolBar addSubview:lineView];
        [toolBar addSubview:cancelButton];
        [toolBar addSubview:confirmButton];
        lineView.backgroundColor = [UIColor colorWithHexString:@"f8f7f7"];
        [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_equalTo(toolBar.mas_centerX);
            make.centerY.mas_equalTo(toolBar.mas_centerY);
            make.width.mas_equalTo(1);
            make.top.mas_equalTo(toolBar.mas_top).mas_offset(10);
            make.bottom.mas_equalTo(toolBar.mas_bottom).mas_offset(-10);
            
        }];
        
        
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancelButton setBackgroundColor:[UIColor colorWithHexString:@"f8f7f7"]];
        [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:13.f]];
        
        [cancelButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.bottom.mas_equalTo(toolBar);
            make.left.mas_equalTo(toolBar.mas_left).mas_offset(15);
            make.right.mas_equalTo(lineView.mas_left);
            
        }];
        [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [confirmButton.titleLabel setFont:[UIFont systemFontOfSize:13.f]];
        confirmButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        
        [confirmButton setTitleColor:[UIColor colorWithHexString:@"dab866"] forState:UIControlStateNormal];
        [confirmButton setBackgroundColor:[UIColor colorWithHexString:@"f8f7f7"]];
        
        [confirmButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.bottom.mas_equalTo(toolBar);
            make.right.mas_equalTo(toolBar.mas_right).mas_offset(-15);
            make.left.mas_equalTo(lineView.mas_right);
            
        }];
        [cancelButton addTarget:self action:@selector(resignFirstResponder) forControlEvents:UIControlEventTouchUpInside];
        [confirmButton addTarget:self action:@selector(selectDataSource:) forControlEvents:UIControlEventTouchUpInside];
        


        return toolBar;
        
    }
    
    return _inputAccessoryView;
    
}
- (void)selectDataSource:(UIBarButtonItem *)sender{
    
    if (self.select_block) {
        self.select_block(self.datePicker.date);
    }
    [self resignFirstResponder];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
